//
//  HSAskDetailVC.m
//  HolyShit
//
//  Created by Robin on 2017/3/29.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSAskDetailVC.h"
#import "HSAskDetailVM.h"
#import "HSAskDetailCell.h"

@interface HSAskDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HSAskDetailVM *askVM;
@property (nonatomic, strong) HSAskDetailCell *tmpCell;

@end

@implementation HSAskDetailVC

- (instancetype)initWithExpertId:(NSString *)expertId
{
    if (self = [super init]) {
        _askVM = [[HSAskDetailVM alloc] initWithExpertId:expertId];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"最热回答";
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.hintView.reloadBlock();
}

- (void)refresh
{
    @weakify(self);
    [[self.askVM.refreshCmd execute:nil] subscribeNext:^(id x) {
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
    }];
}

- (void)loadmore
{
    @weakify(self);
    [[self.askVM.loadmoreCmd execute:nil] subscribeNext:^(id x) {
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        [self.tableView endRefreshAndLoadMore];
    }];
}

#pragma mark - delegate & datasouce

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.askVM.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.askVM.datas[indexPath.row].cellHeight < 1) {
        if (!self.tmpCell) {
            self.tmpCell = [HSAskDetailCell new];
        }
        self.tmpCell.model = self.askVM.datas[indexPath.row];
    }
    return self.askVM.datas[indexPath.row].cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSAskDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSAskDetailCell class])];
    cell.model = self.askVM.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - uitableview

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight)];
        _tableView.backgroundColor = WHITE_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
        [_tableView addLoadMoreTarget:self action:@selector(loadmore)];
        [_tableView registerClass:[HSAskDetailCell class] forCellReuseIdentifier:NSStringFromClass([HSAskDetailCell class])];
    }
    return _tableView;
}


@end
